import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/button/brn_normal_button.dart';
import 'package:smart_devices/bruno/src/components/radio/brn_radio_button.dart';
import 'package:smart_devices/bruno/src/components/toast/brn_toast.dart';
import 'package:smart_devices/model/device_location.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/ui/scroll_refresh_config.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/utils/tools.dart';

import 'location_logic.dart';

class DeviceLocationView extends GetView<DeviceLocationLogic> {
  const DeviceLocationView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DeviceLocationLogic>(
      builder: (_) {
        return Scaffold(
          backgroundColor:
              ColorsUtils.parseColorFromHexString(ColorsUtils.colorF6f6),
          appBar: AppBar(
            leading: IconButton(
              icon: const Icon(
                Icons.arrow_back_ios,
                color: Colors.white,
                size: 15,
              ),
              onPressed: Get.back,
            ),
            automaticallyImplyLeading: false,
            backgroundColor:
                ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
            title: Text(
              '存放位置',
              style: StylesUtils.customTextStyle(
                  fontSize: 18, fontWeight: FontWeight.w700),
            ),
            centerTitle: true,
            systemOverlayStyle: SystemUiOverlayStyle.dark,
          ),
          body: Obx(() => Column(
                children: [
                  Container(
                    height: 50,
                    margin: const EdgeInsets.only(bottom: 15),
                    padding:
                        const EdgeInsets.symmetric(horizontal: 15, vertical: 7),
                    decoration: const BoxDecoration(color: Colors.white),
                    child: TextField(
                      textDirection: TextDirection.ltr,
                      controller: controller.searchController.value,
                      cursorColor: ColorsUtils.parseColorFromHexString(
                          ColorsUtils.color1c24),
                      style: StylesUtils.customTextStyle(
                          fontSize: 16, color: ColorsUtils.color1c24),
                      textAlign: TextAlign.left,
                      textAlignVertical: TextAlignVertical.center,
                      cursorWidth: 2,
                      // cursorHeight: 18,
                      cursorRadius: const Radius.circular(2),
                      decoration: InputDecoration(
                          hintText: '请输入设备编码或者设备名称',
                          hintStyle: StylesUtils.customTextStyle(
                              color: ColorsUtils.colorCccc, fontSize: 13),
                          isDense: true,
                          isCollapsed: true,
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(5),
                              borderSide: BorderSide(
                                  color: ColorsUtils.parseColorFromHexString(
                                      ColorsUtils.colorE4e4))),
                          prefixIcon: Padding(
                            padding: const EdgeInsets.all(10.5),
                            child: ToolsUtils.urlSvg(
                                'https://cdn7.axureshop.com/demo2023/2163241/images/常用组件/u2227.svg',
                                width: 15,
                                height: 15),
                          ),
                          focusColor: ColorsUtils.parseColorFromHexString(
                              ColorsUtils.color1c24),
                          contentPadding:
                              const EdgeInsets.symmetric(vertical: 13),
                          focusedBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(5),
                              borderSide: BorderSide(
                                  color: ColorsUtils.parseColorFromHexString(
                                      ColorsUtils.colorE4e4)))),
                      onChanged: (String text) {
                        controller.search.value = text;
                        controller.searchController.value.text = text;
                      },
                    ),
                  ),
                  Expanded(child: scrollRefreshConfig(context))
                ],
              )),
          extendBody: true,
          bottomSheet: Container(
            height: 70,
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                    top: BorderSide(
                        color: ColorsUtils.parseColorFromHexString(
                            ColorsUtils.colorE4e4)))),
            alignment: Alignment.center,
            child: FractionallySizedBox(
              widthFactor: 0.9,
              heightFactor: 0.75,
              alignment: Alignment.center,
              child: BrnNormalButton(
                onTap: () {
                  DeviceLocationModel model = controller.pageList.firstWhere(
                      (p) => p.checked,
                      orElse: () => DeviceLocationModel(name: ''));
                  if (model.name.isNotEmpty) {
                    return Get.back(result: model.name);
                  }
                  BrnToast.show('请选择位置', context,
                      gravity: BrnToastGravity.bottom);
                },
                backgroundColor:
                    ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
                text: '',
                child: Text(
                  '提交',
                  style: StylesUtils.customTextStyle(fontSize: 14),
                ),
              ),
            ),
          ),
        );
      },
      init: controller,
    );
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
        refreshController: controller.refreshController.value,
        onRefresh: controller.onRefresh,
        onLoading: controller.onLoading,
        child: listViewBuilder());
  }

  Widget listViewBuilder() {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return DrawUnderLine(
          color: controller.pageList.length == index + 1
              ? Colors.transparent
              : ColorsUtils.parseColorFromHexString('#aaaaaa'),
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 15),
            alignment: Alignment.centerLeft,
            color: Colors.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  controller.pageList[index].name,
                  style: StylesUtils.customTextStyle(
                      color: controller.pageList[index].checked
                          ? ColorsUtils.color0052
                          : ColorsUtils.color3333),
                ),
                BrnRadioButton(
                  radioIndex: index,
                  onValueChangedAtIndex: controller.onValueChangedAtIndex,
                  isSelected: controller.pageList[index].checked,
                )
              ],
            ),
          ),
        );
      },
      itemCount: controller.pageList.length,
      itemExtent: 50,
    );
  }
}
